import time
import struct
import asyncio

from tornado.ioloop import IOLoop
from tornado.iostream import PipeIOStream

from message_pipe import get_python_read_fd


async def read_from_pipe():
  pipe_stream = PipeIOStream(get_python_read_fd())

  while True:
    b = await pipe_stream.read_bytes(8)
    size = struct.unpack("=q", b)[0]
    print(size)
    msg = await pipe_stream.read_bytes(size)
    print(msg.decode())


async def print_task():
  while True:
    await asyncio.sleep(1)
    print(time.time())


async def main():
  task1 = asyncio.create_task(print_task())
  task2 = asyncio.create_task(read_from_pipe())
  await task1
  await task2

asyncio.run(main())
